package com.wheelpicker.picker;

import android.os.Bundle;
import android.view.View;

import java.util.List;

import com.wheelpicker.base.BasePickerDialog;
import com.wheelpicker.picker.addresspicker.AddressPickerConfig;
import com.wheelpicker.picker.addresspicker.AddressBean;
import com.wheelpicker.picker.addresspicker.AddressWheel;

/**
 * Created by jzxiang on 16/4/19.
 */
public class AddressPickerDialog extends BasePickerDialog<AddressPickerConfig> {

    private AddressWheel addressWheel;

    private static AddressPickerDialog newInstance(Builder builder) {
        AddressPickerDialog addressPickerDialog = new AddressPickerDialog();
        Bundle bundle = getArgumentBundle(builder);
        addressPickerDialog.setArguments(bundle);
        addressPickerDialog.initialize(builder.mPickerConfig);
        return addressPickerDialog;
    }

    @Override
    protected void initialize(AddressPickerConfig pickerConfig) {
        mPickerConfig = pickerConfig;
    }

    @Override
    protected void initCustomView(View view) {
        addressWheel = new AddressWheel(view, mPickerConfig);
    }

    protected void onSureClicked() {
        AddressBean province = addressWheel.getCurrentProvince();
        AddressBean city = addressWheel.getCurrentCity();
        AddressBean area = addressWheel.getCurrentArea();
        AddressBean town = addressWheel.getCurrentTown();
        String[] values = new String[4];
        if (province != null) {
            values[0] = province.getCode();
        }
        if (city != null) {
            values[1] = city.getCode();
        }
        if (area != null) {
            values[2] = area.getCode();
        }
        if (town != null) {
            values[3] = town.getCode();
        }
        if (mDialogResultListener != null) {
            mDialogResultListener.result(this, values);
        }
    }

    public static class Builder extends BasePickDialogBuild<AddressPickerConfig, Builder, AddressPickerDialog> {


        public Builder() {
            mPickerConfig = new AddressPickerConfig();
        }

        public Builder setType(int type) {
            mPickerConfig.mType = type;
            return this;
        }

        public Builder setAddressProvider(AddressProvider addressProvider) {
            mPickerConfig.addressProvider = addressProvider;
            return this;
        }

        @Override
        public AddressPickerDialog build() {
            return AddressPickerDialog.newInstance(this);
        }

    }

    public abstract static class AddressProvider {

        public List<? extends AddressBean> provinceList, cityList, areaList, streetList;

        public abstract List<? extends AddressBean> getAddressListData(Object parentBean, String parentCode, int type);
    }
}
